/*
 * Copyright (c) 2020
 * User:Palo
 * File:HiPageUtils.java
 * Date:2020/12/25
 */

package com.iceframework.hiapp.core.utils;

import com.iceframework.hiapp.core.constants.SysConfigConstants;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;

import java.util.Map;
import java.util.Set;

public class HiPageUtils {

    /**
     * go slice(平级导航)
     * @param abilitySlice
     * @param cls
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public static void persentSlience(AbilitySlice abilitySlice, Class<? extends AbilitySlice> cls) throws InstantiationException, IllegalAccessException {
        AbilitySlice newAbilitySlice = cls.newInstance();
        abilitySlice.present(newAbilitySlice,new Intent());
    }

    /**
     * go slice(层级导航)
     * get req&data, To term ability slice onResult() function to get
     * @param abilitySlice
     * @param cls
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public static void persentForResultSlience(AbilitySlice abilitySlice, Class<? extends AbilitySlice> cls,String requestCode) throws InstantiationException, IllegalAccessException {
        AbilitySlice newAbilitySlice = cls.newInstance();
        abilitySlice.presentForResult(newAbilitySlice, new Intent(),0);
    }

    public static void openAbilityPage(AbilitySlice abilitySlice, Class<? extends Ability> cls){
        Intent intent =new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(SysConfigConstants.BUNDLE_NAME)
                .withAbilityName(cls.getCanonicalName())
                .build();
        intent.setOperation(operation);
        abilitySlice.startAbility(intent);
    }

    public static void openAbilityPage(AbilitySlice abilitySlice, Class<? extends Ability> cls, Map<String,String> datas){
        Intent intent =new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(SysConfigConstants.BUNDLE_NAME)
                .withAbilityName(cls.getCanonicalName())
                .build();
        intent.setOperation(operation);
        if(datas != null){
            for(String key : datas.keySet()){
                intent.setParam(key, datas.get(key));
            }
        }
        abilitySlice.startAbility(intent);
    }

    /**
     * go action
     * @param abilitySlice
     * @param actionName
     */
    public static void openAction(AbilitySlice abilitySlice, String actionName){
        Intent intent =new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction(actionName)
                .build();
        intent.setOperation(operation);
        abilitySlice.startAbility(intent);
    }

    /**
     * go action with datas
     * @param abilitySlice
     * @param actionName
     * @param datas
     */
    public static void openAction(AbilitySlice abilitySlice, String actionName, Map<String,String> datas){
        Intent intent =new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction(actionName)
                .build();
        intent.setOperation(operation);
        if(datas != null){
            for(String key : datas.keySet()){
                intent.setParam(key, datas.get(key));
            }
        }
        abilitySlice.startAbility(intent);
    }
}
